package com.skycode.servlet;

import com.skycode.bean.NewsBean;
import com.skycode.bean.NoticeBean;
import com.skycode.dao.NewsDao;
import com.skycode.dao.NoticeDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/noticefront.html")
public class NoticeFrontServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");//请求参数类型设置
        String sp = req.getParameter("page");//前端的参数都是String类型
        int page = 1;
        if (sp  != null && !sp.equals("")){//传了情况下
            page = Integer.parseInt(sp);//将前端字符数字强制转化为整形
        }
        NoticeDao newsDao = new NoticeDao();

        List<NoticeBean> list = newsDao.getList(page);//分页查询
        int totaCunt = newsDao.count();//统计总体条数
        //        总共页数
        int totaPage ;
        if (totaCunt%10==0){
            totaPage = totaCunt/10;
        }else {
            totaPage = totaCunt/10+1;
        }
//        上一页
        int upPage;
        if (page==1){
            upPage = 1;
        }else {
            upPage = page - 1;
        }
//        下一页
        int downPage;
        if (page==totaPage){
            downPage = totaPage;
        }else {
            downPage = page + 1;
        }
        req.setAttribute("curPage", page);
        req.setAttribute("upPage", upPage);
        req.setAttribute("downPage", downPage);
        req.setAttribute("totaPage", totaPage);
        req.setAttribute("totaCount", totaCunt);
        req.setAttribute("list", list);//通过req将list数据传递给前端
        // 属于额外知识
        //getRequestDispatcher决定webapp目录系下的那个文件显示
        req.getRequestDispatcher("noticeFrontList.jsp").forward(req, resp);
    }
}
